import { BigDecimal, CollectionUtils } from "@chulei-ui/utils";
import type { UniverseStruct } from "./UniverseStruct";
import type { UniverseStation } from "./UniverseStation";
import type { UniverseConstellation } from "./UniverseConstellation";
import type ExcelJs from "@scvzerng/exceljs";

export class UniverseSystem {
  // 系统ID整数
  system_id!: number;
  // 所在星座的ID
  constellation?: UniverseConstellation;

  // 名称字符串
  name!: string;

  // 安全状态数字
  security_status!: number;

  // 空间站数组，可以包含最多 25 个元素
  stations: UniverseStation[] = [];

  structs: UniverseStruct[] = [];

  static fromSystem(input: any) {
    const system = new UniverseSystem();
    system.security_status = input.security_status;
    system.system_id = input.system_id;
    system.name = input.name;
    return system;
  }

  static fromSheet(sheet: ExcelJs.Worksheet, constellation: UniverseConstellation[]) {
    const systems: UniverseSystem[] = [];
    const constellationMap = CollectionUtils.array2Map<UniverseConstellation>(
      constellation,
      (constellation) => constellation.constellation_id
    );
    sheet.eachRow((row, index) => {
      if (index > 1) {
        const id = row.getCell(1).text!;
        const name = row.getCell(2).text!;
        const constellationId = Number(row.getCell(3).text!);
        const security = row.getCell(7).text!;
        const system = new UniverseSystem();
        const constellation = constellationMap[constellationId];
        system.system_id = Number(id);
        system.name = name;
        system.constellation = constellationMap[constellationId];
        system.security_status = BigDecimal.form(security)!.toFixed(1)?.toNumber()!;
        constellation.systems.push(system);
        systems.push(system);
      }
    });
    return systems;
  }
}
